import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;


public class WC
{

    public static String inputFile;
    public static String outputFile;
    public static int chars;
    public static int words;
    public static int lines;
    public static boolean needC;
    public static boolean needW;
    public static boolean needL;
    public static boolean needO;

    public static void getBasicInfo(String fileName)
    {
        chars=0;
        words=0;
        lines=0;
        char charNow;
        try
        {
            File filename = new File(fileName);
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(filename));
            BufferedReader br = new BufferedReader(reader);
            String line ;
            line = br.readLine();
            while (line != null)
            {
                chars+=line.length();
                lines++;
                boolean partition = true;

                for(int i=0;i<line.length();i++)
                {
                    charNow=line.charAt(i);
                    if(partition==true&&charNow!=' '&&charNow!='\t'&&charNow!=','&&charNow!=',')
                    {
                        words++;
                    }
                    if(charNow==' '||charNow=='\t'||charNow==','||charNow==',')
                    {
                        partition=true;
                    }

                }
                line = br.readLine();
            }
            chars+=lines-1;
            br.close();
        }

        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {


        inputFile="";
        for(int i=0;i<args.length;i++)
        {

            if ("-c".equals(args[i])) {
                needC = true;
            } else if ("-w".equals(args[i])) {
                needW = true;
            } else if ("-l".equals(args[i])) {
                needL = true;
            } else if ("-o".equals(args[i])) {
                needO = true;
                outputFile = args[i + 1];

            } else {
                if (!args[i - 1].equals("-e") && !args[i - 1].equals("-o")) {

                    inputFile = args[i];
                }
            }

        }
        String outputStr="";
        ArrayList<String> fileNames =new ArrayList<String>();
        fileNames.add(inputFile);
        int len=fileNames.size();
        String fn;

        for(int i=0;i<len;i++)
        {
            fn=fileNames.get(i);
            String fileShortName=fn.substring(fn.lastIndexOf("\\")+1, fn.length());
            if(needC||needW||needL)
            {

                getBasicInfo(fn);


                if(needC)
                {

                    outputStr+=fileShortName;
                    outputStr+=", char: ";
                    outputStr+=chars;
                    outputStr+="\r\n";
                }
                if(needW)
                {

                    outputStr+=fileShortName;
                    outputStr+=", word: ";
                    outputStr+=words;
                    outputStr+="\r\n";
                }
                if(needL)
                {

                    outputStr+=fileShortName;
                    outputStr+=", line: ";
                    outputStr+=lines;
                    outputStr+="\r\n";
                }
            }


        }

        if(!needO)
        {
            outputFile="result.txt";
        }
        try
        {

            File writename = new File(outputFile);
            writename.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(writename));
            out.write(outputStr);
            out.flush();
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

}